
rm(list = ls())
library(tidyverse)
library(dplyr)
library(rlang)
library(purrr)
library(readxl)
library(haven)
library(readxl)

#set your own directory please!

#setwd("~/Dropbox (Harvard University)/Gov 2001 Rep Paper/R Scripts")

#setwd("~/Dropbox (Harvard University)/Gov 2001 Rep Paper/R Scripts")

years <- seq(1918,2001,1)

###################################################
######### Polity IV ###########
###################################################

polity <- read_por("Datasets/Polity III Dem/DS0001/06695-0001-Data.por") %>% 
        dplyr::select(YEAR, COUNTRY, AUTOC, DEMOC, XRCOMP, PARREG) %>% filter(YEAR %in% years)
polity <- polity %>% mutate(polity,nameb = COUNTRY)

saveRDS(polity, "polityIII.RDS")


polity2 <- read_excel("Datasets/polity2.xls")


polity2 <- polity2%>%dplyr::select(scode, year,polity2)%>%filter(year %in% years) %>% mutate(polity2, demo = if_else(polity2 >5, 1, 0))

saveRDS(polity2, "polity2.RDS")

# Democracies are coded 1 if xrcomp = 2 or 3 and parreg = 2 or 5.
# A bit confused as to how they added the other Dem datasets, will need to check. 


###################################################
######### Boix Dem ##################
###################################################

boix <- read_dta("Datasets/Boix Dem/democracy-v3.0.dta") %>%
    dplyr::select (ccode, abbreviation, year, democracy) %>% filter(year %in% years)


saveRDS(boix, "boix.RDS")

###################################################
######### Cheibub Dem ##################
###################################################

cheibub <- read_dta("Datasets/Cheibub/ddrevisited_data_v1.dta") %>% 
  dplyr::select(year,democracy,ccdcodelet) %>% filter(year %in% years)

saveRDS(cheibub, "Cheibub.RDS")

###################################################
######### Przeworksi Dem ##################
###################################################

prze <- read.csv("Datasets/Przeworski Dem/AclpData.csv") %>%
  dplyr::select(YEAR,REG, COUNTRY) %>% filter(YEAR %in% years)

saveRDS(prze, "Prze.RDS")

#######need to merge country number with COW country abbr#####

###################################################
######### Vanhanen Dem ##################
###################################################

Vanhanen <- read_dta("Datasets/Vanhanen Dem.dta") %>% 
  dplyr::select(year, abbr, comp) %>% filter(year %in% years)

saveRDS(Vanhanen, "Vanhanen.RDS")


###################################################
######### Contiguity  ##################
###################################################

contig <-read_csv("Datasets/Direct Contig/contdird.csv")%>% 
    dplyr::select(year, state1ab, state2ab, conttype) 
contig <- contig%>% mutate(contig, conttype = if_else(conttype > 1 & conttype <5, 1,0))

saveRDS(contig, "contig.RDS")


###################################################
######### Military Capabilities  ##################
###################################################

cinc <- read_csv("Datasets/power index/NMC_5_0.csv") %>% 
  dplyr::select(year, cinc, stateabb) %>% filter(year %in% years)

saveRDS(cinc, "cinc.RDS")


###################################################
######### Major Powers   ##################
###################################################

##need to set up ourselves 

years_USA_GB <- seq(1918, 1993,1) 

years_FR <- sort(c(seq(1918, 1940,1), seq(1945,1993,1)))

years_GR <- sort(c(1918,seq(1925,1945,1), seq(1990,1993,1)))

years_AH <- 1918

years_IT <- seq(1918,1943,1)

years_RU <- seq(1922,1993,1)

years_CH <- seq(1950,1993,1)

years_JP <- sort(c (seq(1918,1945,1),seq(1990,1993,1)))


states <- c("USA", "UKG", "FRN", "GMY", "AUH", "RUS","CHN", "JPN","ITA")

mids<-read_csv("Datasets/MIDs Dyads/dyadic MIDs 3.1.csv") %>% filter(strtyr %in% years)

gp <- mids%>%filter(namea %in% states)%>%filter(strtyr %in% years)

USA_GB <- gp%>%filter(namea %in% c("USA","UKG")) %>%filter(strtyr %in% years_USA_GB)

FR <- gp%>%filter(namea =="FRN") %>%filter(strtyr %in% years_FR)

GR <- gp%>%filter(namea == "GMY") %>%filter(strtyr %in% years_GR)

AH <- gp %>%filter(namea == "AUH")%>%filter(strtyr %in% years_AH)

IT <- gp %>%filter(namea == "ITA")%>%filter(strtyr %in% years_IT)

RU <- gp %>%filter(namea == "RUS")%>%filter(strtyr %in% years_RU)

CH <- gp %>%filter(namea == "CHN")%>%filter(strtyr %in% years_CH)

JP <- gp %>%filter(namea == "JPN")%>%filter(strtyr %in% years_JP)


maj_pwr<-rbind(USA_GB, FR, GR,AH,IT,RU,CH,JP)

mids_0 <- setdiff(mids, maj_pwr) 

mids_1 <- setdiff(mids, mids_0) 

mids_1<-mids_1%>% mutate(mids_1, major_power=1)

mids_0 <- mids_0 %>% mutate(mids_0, major_power=0)

major_power<- rbind(mids_1,mids_0) %>%dplyr::select(namea, major_power,strtyr, strtmnth,strtday,endyear,endday,endmnth)

saveRDS(major_power, "major_power.RDS")


###################################################
######### Alliance Similarity  ##################
###################################################

##MIDS only has the initiator state?

alliance <- read_dta("Datasets/Alliance similarity/atop-sscore.dta") %>% 
  dplyr::select(year,cabb1, cabb2,s_wt_atop) %>% filter(year %in% years)


saveRDS(alliance, "alliance.RDS")



###################################################
######### Nuclear  ##################
###################################################


##need to set ourselves


years_USA <- seq(1945, 2001,1) 
years_GB <- seq(1952, 2001,1)
years_FR <- seq(1960, 2001,1)
years_CH <- seq(1964, 2001,1)
years_IN <- seq(1974, 2001,1)
years_IS <- seq(1969,2001,1)
years_PK <- seq(1992, 2001,1)

##they left out Russia but I'll code it 

years_RU<- seq(1949, 2001,1) 

states <- c("USA", "UKG", "FRN","RUS","CHN", "PAK","ISR", "IND")

mids<-read_csv("Datasets/MIDs Dyads/dyadic MIDs 3.1.csv") %>% filter(strtyr %in% years)

gp <- mids%>%filter(namea %in% states)%>%filter(strtyr %in% years)

US <- gp%>%filter(namea=="USA") %>%filter(strtyr %in% years_USA)
GB <- gp%>%filter(namea=="UKG") %>%filter(strtyr %in% years_GB)
FR <- gp%>%filter(namea=="FRN") %>%filter(strtyr %in% years_FR)
CH <- gp%>%filter(namea=="CHN") %>%filter(strtyr %in% years_CH)
IN <- gp%>%filter(namea=="IND") %>%filter(strtyr %in% years_IN)
IS <- gp%>%filter(namea=="ISR") %>%filter(strtyr %in% years_IS)
PK <- gp%>%filter(namea=="PAK") %>%filter(strtyr %in% years_PK)
RU<- gp%>%filter(namea=="RUS") %>%filter(strtyr %in% years_RU)


nuc_pr<-rbind(US, FR, GB,CH,IN,IS,PK,RU)

mids_0 <- setdiff(mids, nuc_pr) 

mids_1 <- setdiff(mids, mids_0) 
mids_1 <- mids_1 %>% mutate(mids_1, nuc_pr=1)

mids_0 <- mutate(mids_0, nuc_pr=0)

nuclear_power<- rbind(mids_1,mids_0) %>%dplyr::select(namea, nuc_pr,strtyr, strtmnth,strtday,endyear,endday,endmnth)

saveRDS(nuclear_power, "nuclear_power.RDS")

###################################################
######### Resolve  ##################
###################################################

mids1 <-read.csv("Datasets/MID/MIDB 4.3.csv") %>% filter(styear %in% years) 
mids1 <- mids1 %>% mutate(mids1,resolve = 0)

for (i in 1:length(mids1$resolve)) {
  if (mids1$hostlev[i] <= 2) {
    mids1$resolve[i] <- 0}
  if (mids1$hostlev[i] == 3){
    mids1$resolve[i] <- 1}
if (mids1$hostlev[i] >= 4) {
    mids1$resolve[i] <- 2
  }
}


resolve <- mids1%>%dplyr::select(styear,stabb,resolve)

saveRDS(resolve, "resolve.RDS")



